home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 11 / Cream of the Crop 11-1.iso / disk / hdctrl.zip / HDCTRL.C < prev    next >
C/C++ Source or Header  |  1995-12-12  |  15KB  |  669 lines

  1. /* ------------------------------------------------------------------------ */
  2. /* Hard Disk Maintenance                                                    */
  3. /* by Ricard Forner           Internet: <se03421@els.url.es>                */
  4. /*                            Fidonet:  Ricard_Forner, 2:343/130.5          */
  5. /*                                                                          */
  6. /* You may freely use or incorporate these routines into your own programs  */
  7. /* without royalty to me, as I believe this is beneficial to programmers.   */
  8. /* However, I would like to request that if you distribute the source code, */
  9. /* you would include this header in the source file and not remove it.      */
  10. /* Of course, I would have no way of knowing, but I am appealing to your    */
  11. /* sense of good will and morality.  Thank you, and I hope these routines   */
  12. /* are useful.                                                              */
  13. /*                                                                          */
  14. /* December, 1995 - Version 1.0                                 [FREEWARE]  */
  15. /* ------------------------------------------------------------------------ */
  16.  
  17.  
  18. #include <conio.h>
  19. #include <dir.h>
  20. #include <dos.h>
  21. #include <io.h>
  22. #include <stdio.h>
  23. #include <stdlib.h>
  24. #include <string.h>
  25. #include <sys\stat.h>
  26.  
  27. #define FALSE 0
  28. #define TRUE  1
  29.  
  30. /* -------------------------------------------------------------------- */
  31.  
  32. struct Card {
  33.    char subpath[MAXPATH];
  34.    long jump;
  35.    struct Card *pointer;
  36. } *capf, *pf, *qf;
  37.  
  38. struct _Drive_ {
  39.    int hdinit;
  40.    int hdnumber;
  41. } driveinit;
  42.  
  43. char StatusTXT[4] = { '|', '/', '-', '\\'};
  44. char StatusNUM;
  45. char final;
  46. char ophelp;
  47. char option[3];
  48. char filename[15];
  49. char TextLog[5][32];
  50. char TextError[5][44];
  51. int  numerror;
  52. int  drive;
  53. int  ch;
  54.  
  55. FILE* FileWork;
  56. FILE* FileWorkLog;
  57.  
  58. /* -------------------------------------------------------------------- */
  59.  
  60. int      NumberOfDrives   (void) ;
  61. void  MAIN_Start       (void) ;
  62. void  MAIN_End           (void) ;
  63. void  MAIN_Parameters  (int, char**) ;
  64. void  MAIN_Operate       (void) ;
  65. void  Help               (void) ;
  66. void  Concatena        (void) ;
  67. void  DIR_Dir           (struct ffblk) ;
  68. void  DIR_Write           (struct ffblk) ;
  69. void  DIR_File           (struct ffblk) ;
  70. void  DIR_All           (struct ffblk) ;
  71. void  LIST_Create       (char*) ;
  72. void  LIST_Delete      (void) ;
  73. char  LIST_Search      (struct Card*, char*) ;
  74. void  FILE_Create      (char*) ;
  75. void  FILE_CreateLog   (void) ;
  76. void  FILE_WritePath   (void) ;
  77. void  FILE_WriteFile   (struct ffblk) ;
  78. void  FILE_ReadAll     (void) ;
  79. char* FILE_ReadStr     (void) ;
  80. void  FILE_JumpChar    (void) ;
  81. void  Status           (void) ;
  82. void  FileNoDir        (struct ffblk, int) ;
  83. void  OptionCreate     (void) ;
  84. void  OptionMaint      (void) ;
  85. void  OptionLook       (void) ;
  86. void  LookAll          (void) ;
  87. void  LookPath         (char*, struct Card*) ;
  88. void  LookNoIs         (char*, int) ;
  89.  
  90. /* -------------------------------------------------------------------- */
  91.  
  92. void main (int ac, char** av)
  93. {
  94.    MAIN_Start();
  95.    MAIN_Parameters(ac, av);
  96.    MAIN_Operate();
  97.    MAIN_End();
  98. }
  99.  
  100. /* */
  101.  
  102. int NumberOfDrives(void)
  103. {
  104. int save, disk, num_d=0;
  105.  
  106.    save=getdisk();
  107.    setdisk(save);
  108.    for (disk=0; disk<26; ++disk)
  109.    {
  110.       setdisk(disk);
  111.       if (disk == getdisk()) num_d++;
  112.    }
  113.    setdisk(save);
  114.    return num_d-2;
  115. }
  116.  
  117. /* */
  118.  
  119. void MAIN_Start(void)
  120. {
  121.    capf=pf=qf=NULL;
  122.    ophelp=final=FALSE;
  123.    option[0]=option[1]=option[2]=FALSE;
  124.    drive=numerror=-1;
  125.    StatusNUM=0;
  126.    strcpy(TextError[0]," Memory exhausted.");
  127.    strcpy(TextError[1]," Destination file did not create.");
  128.    strcpy(TextError[2]," Operating system error.");
  129.    strcpy(TextError[3]," Archive not found.");
  130.    strcpy(TextError[4]," Archive incorrect.");
  131.    strcpy(TextLog[0],"[HDCTRL.LOG v1.00]");
  132.    strcpy(TextLog[1],"  Directory deleted..:  ");
  133.    strcpy(TextLog[2],"  File deleted.......:  ");
  134.    strcpy(TextLog[3],"  Miss (directory)...:  ");
  135.    strcpy(TextLog[4],"  Miss (file)........:  ");
  136.    driveinit.hdinit = getdisk();
  137.    driveinit.hdnumber = NumberOfDrives();
  138.    setdisk(driveinit.hdinit);
  139.  
  140.    _setcursortype(_NOCURSOR);
  141.    printf("\n");
  142.    printf("HDCTRL.EXE v1.00\n");
  143.    printf("by Ricard Forner\n");
  144. }
  145.  
  146. /* */
  147.  
  148. void MAIN_End(void)
  149. {
  150.    LIST_Delete();
  151.    setdisk(driveinit.hdinit);
  152.    getdisk();
  153.    _setcursortype(_NORMALCURSOR);
  154.    if ((drive>-1) && (ophelp==FALSE) && (numerror<0))
  155.       if (driveinit.hdinit==(drive+2)) chdir("\\");
  156. }
  157.  
  158. /* */
  159.  
  160. void MAIN_Parameters(int argc, char** argv)
  161. {
  162. char i, j, texttmp[3];
  163.  
  164.    strcpy(texttmp, "A:\0");
  165.    for (i=1; i<argc; i++)
  166.    {
  167.       strupr(argv[i]);
  168.       for (j=0; j<driveinit.hdnumber; j++)
  169.       {
  170.          texttmp[0]=j+'C';
  171.          if (strstr(argv[i], texttmp)) { drive=j; break; }
  172.       }
  173.       if (strstr(argv[i], "/?")) { ophelp=TRUE; break; }
  174.       if (strstr(argv[i], "/C")) { option[0]=TRUE; continue; }
  175.       if (strstr(argv[i], "/R")) { option[1]=TRUE; continue; }
  176.       if (strstr(argv[i], "/M")) { option[2]=TRUE; continue; }
  177.    }
  178.    if ((option[0]==FALSE) && (option[1]==FALSE) && (option[2]==FALSE)) ophelp=TRUE;
  179.    if (drive<0) ophelp=TRUE;
  180. }
  181.  
  182. /* */
  183.  
  184. void MAIN_Operate(void)
  185. {
  186.    if (ophelp==TRUE) { Help(); return; }
  187.    if (option[0]==TRUE) { OptionCreate(); return; }
  188.    if (option[1]==TRUE) { OptionMaint(); return; }
  189.    if (option[2]==TRUE) { OptionLook(); return; }
  190. }
  191.  
  192. /* */
  193.  
  194. void Help(void)
  195. {
  196.    printf("\n");
  197.    printf("Usage: HDCTRL.EXE  drive: /C|R|M|?\n");
  198.    printf("\n");
  199.    printf("             drive:   Logical drive\n");
  200.    printf("             /C       Create archive\n");
  201.    printf("             /R       Maintenance\n");
  202.    printf("             /M       Which files were missed ?\n");
  203.    printf("             /?       This help\n");
  204.    printf("\n");
  205. }
  206.  
  207. /* */
  208.  
  209. void error (int num)
  210. {
  211.    numerror=num;
  212.    printf("\nERROR:%s", TextError[num]);
  213.    MAIN_End();
  214.    exit(0);
  215. }
  216.  
  217. /* */
  218.  
  219. void Concatena(void)
  220. {
  221. char *file_tmp = "\\Hdctrl.___\0";
  222.  
  223.    strcpy(filename, file_tmp);
  224.    filename[strlen(filename)-2] = drive + 'C';
  225. }
  226.  
  227. /* */
  228.  
  229. void DIR_Dir(struct ffblk p)
  230. {
  231. int fin;
  232.     fin = findfirst("*.*", &p, (FA_DIREC | FA_HIDDEN | FA_SYSTEM));
  233.     DIR_Write(p);
  234.     while (!fin)
  235.     {
  236.         if (p.ff_name[0]!='.')
  237.         {
  238.             if (p.ff_attrib & FA_DIREC)
  239.             {
  240.                if (chdir(p.ff_name)) error(2);
  241.                DIR_Write(p);
  242.                DIR_Dir(p);
  243.             }
  244.         }
  245.         fin = findnext(&p);
  246.     }
  247.     chdir("..");
  248. }
  249.  
  250. /* */
  251.  
  252. void DIR_Write(struct ffblk p)
  253. {
  254. char buffer[MAXPATH];
  255.  
  256.    getcwd(buffer, MAXPATH); strupr(buffer);
  257.    if (p.ff_name[0]!='.') LIST_Create(buffer);
  258. }
  259.  
  260. /* */
  261.  
  262. void DIR_File(struct ffblk p)
  263. {
  264. int fin;
  265.  
  266.    fin = findfirst("*.*", &p, (FA_RDONLY | FA_HIDDEN | FA_SYSTEM | FA_DIREC | FA_ARCH));
  267.    if (!fin)
  268.    {
  269.       FILE_WritePath();
  270.       while (!fin)
  271.       {
  272.          if (p.ff_name[0]!='.')
  273.          {
  274.             if (!(p.ff_attrib & FA_DIREC)) FILE_WriteFile(p);
  275.          }
  276.          fin = findnext(&p);
  277.          Status();
  278.       }
  279.    }
  280. }
  281.  
  282. /* */
  283.  
  284. void DIR_All(struct ffblk p)
  285. {
  286. int fin;
  287.     fin = findfirst("*.*", &p, (FA_RDONLY | FA_HIDDEN | FA_SYSTEM | FA_DIREC | FA_ARCH));
  288.     while (!fin)
  289.     {
  290.         if (p.ff_name[0]!='.')
  291.         {
  292.             if (p.ff_attrib & FA_DIREC)
  293.             {
  294.                   if (chdir(p.ff_name)) error(2);
  295.                   DIR_All(p);
  296.                   FileNoDir(p,FALSE);
  297.             }
  298.             else { FileNoDir(p,TRUE); }
  299.         }
  300.         fin = findnext(&p);
  301.         Status();
  302.     }
  303.     chdir("..");
  304. }
  305.  
  306. /* */
  307.  
  308. void LIST_Create(char *name)
  309. {
  310.    if (capf==NULL)
  311.    {
  312.       capf = (struct Card*)malloc(sizeof(struct Card));
  313.       pf = capf;
  314.    }
  315.    else
  316.    {
  317.       qf = (struct Card*)malloc(sizeof(struct Card));
  318.       pf->pointer = qf; pf = qf;
  319.    }
  320.    if (pf==NULL) error(0);
  321.    pf->pointer = NULL;
  322.    if ((option[1]==TRUE) || (option[2]==TRUE)) pf->jump = ftell(FileWork);
  323.    strcpy(pf->subpath, name);
  324. }
  325.  
  326. /* */
  327.  
  328. void LIST_Delete(void)
  329. {
  330.    pf = capf;
  331.    while (capf!=NULL)
  332.    {
  333.       capf = pf->pointer;
  334.       free(pf);
  335.       pf = capf;
  336.    }
  337. }
  338.  
  339. /* */
  340.  
  341. char LIST_Search(struct Card* valuef, char *name)
  342. {
  343. char path_Ok;
  344.  
  345.    path_Ok = FALSE;
  346.    qf = capf;
  347.    while (qf!=NULL)
  348.    {
  349.       if (strcmp(qf->subpath, name)==0)
  350.       {
  351.          path_Ok = TRUE;
  352.          break;
  353.       }
  354.       else path_Ok = FALSE;
  355.       qf = qf->pointer;
  356.    }
  357.    *valuef=*qf;
  358.    return path_Ok;
  359. }
  360.  
  361. /* */
  362.  
  363. void FILE_Create(char* mode)
  364. {
  365.    FileWork=fopen(filename, mode);
  366.    if (FileWork == NULL) error(1);
  367. }
  368.  
  369. /* */
  370.  
  371. void FILE_CreateLog(void)
  372. {
  373. char *file_temp = "\\Hdctrl.Log\0";
  374.  
  375.    FileWorkLog=fopen(file_temp, "wt");
  376.    if (FileWorkLog == NULL) error(1);
  377.  
  378.    fputc(10, FileWorkLog);
  379.    fputs(TextLog[0], FileWorkLog);
  380.    fputc(10, FileWorkLog);
  381. }
  382.  
  383. /* */
  384.  
  385. void FILE_WritePath(void)
  386. {
  387. char buffer[MAXPATH];
  388.  
  389.    getcwd(buffer, MAXPATH); strupr(buffer);
  390.    if (buffer[strlen(buffer)-1]!='\\') strcat(buffer, "\\\0");
  391.    fprintf(FileWork, "%s", buffer);
  392.    fprintf(FileWork, "%c%c", 254, 10);
  393.    if (buffer[3]=='\0') fprintf(FileWork, "HDCTRL.LOG%c", 10);
  394. }
  395.  
  396. /* */
  397.  
  398. void FILE_WriteFile(struct ffblk p)
  399. {
  400.    if (p.ff_name[0]!='.')
  401.    {
  402.       if (!(p.ff_attrib & FA_DIREC))
  403.       {
  404.          fprintf(FileWork, "%s%c", strupr(p.ff_name), 10);
  405.       }
  406.    }
  407. }
  408.  
  409. /* */
  410.  
  411. void FILE_ReadAll(void)
  412. {
  413. char *nametmp;
  414. char nameok[MAXPATH];
  415.  
  416.    ch = fgetc(FileWork);
  417.    nametmp = FILE_ReadStr();
  418.    strcpy(nameok, nametmp);
  419.    if (strcmp(nameok, "∙_DIR_∙\0")) error(4);
  420.    while ((!feof(FileWork)) && (final==FALSE))
  421.    {
  422.       nametmp = FILE_ReadStr();
  423.       strcpy(nameok, nametmp);
  424.       LIST_Create(nameok);
  425.       FILE_JumpChar();
  426.    }
  427. }
  428.  
  429. /* */
  430.  
  431. char* FILE_ReadStr(void)
  432. {
  433. char name[MAXPATH];
  434. unsigned char i;
  435.  
  436.    i=0;
  437.    while ( (!feof(FileWork)) && (ch!=10) )
  438.    {
  439.       name[i++] = ch;
  440.       ch = fgetc(FileWork);
  441.    }
  442.    name[i]='\0';
  443.    if (feof(FileWork)) { final=TRUE; }
  444.    else { ch=fgetc(FileWork); }
  445.    return name;
  446. }
  447.  
  448. /* */
  449.  
  450. void FILE_JumpChar(void)
  451. {
  452. char *name;
  453.  
  454.    while ( (!feof(FileWork)) && (final==FALSE) )
  455.    {
  456.       name = FILE_ReadStr();
  457.       if (!strcmp(name, "∙_DIR_∙\0")) break;
  458.    }
  459. }
  460.  
  461. /* */
  462.  
  463. void Status(void)
  464. {
  465.    gotoxy(wherex()-3, wherey()); printf("<%c>", StatusTXT[StatusNUM]);
  466.    StatusNUM=(StatusNUM==3)?0:StatusNUM+1;
  467. }
  468.  
  469. /* */
  470.  
  471. void FileNoDir(struct ffblk p, int isafile)
  472. {
  473. char buffer[MAXPATH], *bar_inv="\\\0";
  474. char name[MAXPATH];
  475. char find;
  476. struct Card cardtmp;
  477. int i;
  478.  
  479.    find = FALSE;
  480.    getcwd(buffer, MAXPATH);
  481.    if (buffer[strlen(buffer)-1]!='\\') strcat(buffer, bar_inv);
  482.    strcat(buffer, "■\0");
  483.  
  484.    if (isafile==FALSE)
  485.    {
  486.       strncpy(buffer, buffer, strlen(buffer)-2);
  487.       buffer[strlen(buffer)-1]='\0';
  488.       strcat(buffer, p.ff_name);
  489.       if (buffer[strlen(buffer)-1]!='\\') strcat(buffer, bar_inv);
  490.       strcat(buffer, "■\0");
  491.       find = LIST_Search(&cardtmp, buffer);
  492.    }
  493.    else {
  494.       if (LIST_Search(&cardtmp, buffer)==TRUE)
  495.       {
  496.          rewind(FileWork);
  497.          final = FALSE;
  498.          fseek(FileWork, cardtmp.jump-1, SEEK_SET);
  499.          ch = fgetc(FileWork);
  500.          strcpy(name, FILE_ReadStr());
  501.          while ((final==FALSE) && (strcmp(name, "∙_DIR_∙\0")))
  502.          {
  503.             if (strcmp(name, p.ff_name)==0) { find=TRUE; break; }
  504.             strcpy(name, FILE_ReadStr());
  505.             find=FALSE;
  506.          }
  507.       }
  508.       strncpy(buffer, buffer, strlen(buffer)-2);
  509.       buffer[strlen(buffer)-1]='\0';
  510.       strcat(buffer, p.ff_name);
  511.    }
  512.    if (find==FALSE)
  513.    {
  514.       if (p.ff_attrib & FA_RDONLY) if (chmod(p.ff_name, S_IWRITE)) error(2);
  515.       if (isafile==TRUE)
  516.       {
  517.          if (unlink(p.ff_name)) error(2);
  518.          fputs(TextLog[2], FileWorkLog);
  519.          fputs(buffer, FileWorkLog);
  520.          fputc(10, FileWorkLog);
  521.       }
  522.       else
  523.       {
  524.          if (rmdir(p.ff_name)) error(2);
  525.          buffer[strlen(buffer)-2]='\0';
  526.          fputs(TextLog[1], FileWorkLog);
  527.          fputs(buffer, FileWorkLog);
  528.          fputc(10, FileWorkLog);
  529.       }
  530.    }
  531. }
  532.  
  533. /* */
  534.  
  535. void OptionCreate(void)
  536. {
  537. struct ffblk p;
  538.  
  539.    Concatena();
  540.    setdisk(drive+2);
  541.    if (chdir("\\")) error(2);
  542.    printf("In process ...");
  543.    DIR_Dir(p);
  544.    pf = capf;
  545.    if (pf!=NULL) FILE_Create("wt");
  546.    while (pf!=NULL)
  547.    {
  548.       if (chdir("\\")) error(2);
  549.       if (chdir(pf->subpath)) error(2);
  550.       fprintf(FileWork, "%c_DIR_%c%c", 249, 249, 10);
  551.       DIR_File(p);
  552.       pf = pf->pointer;
  553.    }
  554.    if (chdir("\\")) error(2);
  555.    gotoxy(wherex()-3, wherey()); printf(" OK\n");
  556.    printf("File %c:\\HDCTRL._%c_ created\n", drive+'C', drive+'C');
  557.    fclose(FileWork);
  558. }
  559.  
  560. /* */
  561.  
  562. void OptionMaint(void)
  563. {
  564. struct ffblk p;
  565.  
  566.    Concatena();
  567.    setdisk(drive+2);
  568.    if (access(filename, 0)) error(3);
  569.    if (chdir("\\")) error(2);
  570.    printf("In process ...");
  571.    FILE_Create("rt");
  572.    FILE_CreateLog();
  573.    FILE_ReadAll();
  574.    rewind(FileWork);
  575.    DIR_All(p);
  576.    fputs("[EOF]", FileWorkLog);
  577.    fputc(10, FileWorkLog);
  578.    if (chdir("\\")) error(2);
  579.    gotoxy(wherex()-3, wherey()); printf(" OK\n");
  580.    printf("File %c:\\HDCTRL.LOG created\n", drive+'C');
  581.    fclose(FileWork);
  582. }
  583.  
  584. /* */
  585.  
  586. void OptionLook(void)
  587. {
  588.    Concatena();
  589.    setdisk(drive+2);
  590.    if (access(filename, 0)) error(3);
  591.    if (chdir("\\")) error(2);
  592.    printf("In process ...");
  593.    FILE_Create("rt");
  594.    FILE_ReadAll();
  595.    FILE_CreateLog();
  596.    LookAll();
  597.    if (chdir("\\")) error(2);
  598.    gotoxy(wherex()-3, wherey()); printf(" OK\n");
  599.    printf("File %c:\\HDCTRL.LOG created\n", drive+'C');
  600.    fclose(FileWork);
  601.    fclose(FileWorkLog);
  602. }
  603.  
  604. /* */
  605.  
  606. void LookAll(void)
  607. {
  608. char buffer[MAXPATH];
  609. struct Card *cardtmp;
  610.  
  611.    rewind(FileWork);
  612.    cardtmp = capf;
  613.    while (cardtmp!=NULL)
  614.    {
  615.       strcpy(buffer, cardtmp->subpath);
  616.       buffer[strlen(buffer)-1]='\0';
  617.       LookPath(buffer, cardtmp);
  618.       cardtmp = cardtmp->pointer;
  619.    }
  620.    fputs("[EOF]", FileWorkLog);
  621.    fputc(10, FileWorkLog);
  622. }
  623.  
  624. /* */
  625.  
  626. void LookPath(char *namepath, struct Card *temp)
  627. {
  628. char namefile[MAXPATH];
  629. char destino[MAXPATH];
  630.  
  631.    Status();
  632.    strcpy(destino, namepath); destino[strlen(destino)-1] = '\0';
  633.    if (access(destino, 0)!=FALSE) LookNoIs(destino, FALSE);
  634.    final = FALSE;
  635.    fseek(FileWork, temp->jump-1, SEEK_SET);
  636.    ch = fgetc(FileWork);
  637.    strcpy(namefile, FILE_ReadStr());
  638.    while ( (final==FALSE) && (strcmp(namefile, "∙_DIR_∙\0")) )
  639.    {
  640.       strcpy(destino, namepath);
  641.       strcat(destino, namefile);
  642.       if (access(destino, 0)!=FALSE) LookNoIs(destino, TRUE);
  643.       Status();
  644.       strcpy(namefile, FILE_ReadStr());
  645.    }
  646.    if (final==TRUE)
  647.    {
  648.       if (strlen(namefile))
  649.       {
  650.          strcpy(destino, namepath);
  651.          strcat(destino, namefile);
  652.          if (access(destino, 0)!=FALSE) LookNoIs(destino, TRUE);
  653.       }
  654.    }
  655. }
  656.  
  657. /* */
  658.  
  659. void LookNoIs(char *name, int isafile)
  660. {
  661.    if (strlen(name)==2) return;
  662.    if (isafile==FALSE) fputs(TextLog[3], FileWorkLog);
  663.    else fputs(TextLog[4], FileWorkLog);
  664.    fputs(name, FileWorkLog);
  665.    fputc(10, FileWorkLog);
  666. }
  667.  
  668. /* [EOF] */
  669.